Crate routefinder[][src]

Expand description

Routefinder

use routefinder::{Router, Captures};
let mut router = Router::new();
router.add("/*", 1)?;
router.add("/hello", 2)?;
router.add("/:greeting", 3)?;
router.add("/hey/:world", 4)?;
router.add("/hey/earth", 5)?;
router.add("/:greeting/:world/*", 6)?;

assert_eq!(*router.best_match("/hey/earth").unwrap(), 5);
assert_eq!(
    router.best_match("/hey/mars").unwrap().captures().get("world"),
    Some("mars")
);
assert_eq!(router.matches("/hello").len(), 3);
assert_eq!(router.matches("/").len(), 1);

// reverse lookup
let captures = Captures::from(vec![("world", "jupiter")]);
let reverse_match = router.best_reverse_match(&captures).unwrap();
assert_eq!(*reverse_match, 4);
assert_eq!(reverse_match.to_string(), "/hey/jupiter");

Check out Router for a good starting place

Structs

Capture

An individual key-value pair

Captures

Captured params and a wildcard

Match

The output of a successful application of a Route to a str path, as well as references to any captures.

ReverseMatch

This struct represents the result of a reverse lookup from Captures to a Route

Route

A parsed RouteSpec and associated handler

RouteSpec

Routefinder’s representation of the parsed route

Router

The top level struct for routefinder

Enums

Segment

the internal representation of a parsed component of a route